﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using Chromely.CefGlue.Browser;
using Chromely.Core;
using Chromely.Core.Configuration;
using Chromely.Core.Host;
using Chromely.Native;
using Reader.Core;
using Xilium.CefGlue;
using Xilium.CefGlue.Wrapper;
using static Chromely.Native.WinNativeMethods;

namespace Reader.Controllers.Message
{
    public class WindowCommandsController: ProcessController
    {
        private readonly IChromelyConfiguration _config;
        private readonly IChromelyWindow _window;

        [DllImport("user32.dll")]
        public static extern bool LockWindowUpdate(IntPtr hWndLock);
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

        private bool _fullscreen = false;
        private IntPtr dwStyle;
        private WINDOWPLACEMENT wp;



        public WindowCommandsController(IChromelyConfiguration config, IChromelyWindow window)
        {
            _config = config;
            _window = window;
        }
        public void min()
        {
            ShowWindow(ShowWindowCommand.SW_MINIMIZE);
        }
        public void max()
        {
            WINDOWPLACEMENT wPlaceMent = new WINDOWPLACEMENT();
            GetWindowPlacement(_window.Handle, ref wPlaceMent);
            if (wPlaceMent.ShowCmd == ShowWindowCommands.Maximized)
            {
                ShowWindow(ShowWindowCommand.SW_RESTORE);
            }
            else
            {
                ShowWindow(ShowWindowCommand.SW_MAXIMIZE);
            }
        }
        private void ShowWindow(ShowWindowCommand cmdShow)
        {
            if(_config.Platform == ChromelyPlatform.Windows)
            {
                var handler = _window.Handle;
                LockWindowUpdate(handler);
                if (_fullscreen)
                {
                    fullscreen();
                }
                WinNativeMethods.ShowWindow(handler, cmdShow);
                LockWindowUpdate(IntPtr.Zero);
            }
        }
        public void fullscreen()
        {
            if(_config.Platform == ChromelyPlatform.Windows)
            {
                var handler = _window.Handle;
                if (_fullscreen)
                {
                    wp.ShowCmd = (ShowWindowCommands)9; //Restored
                    SetWindowLong(handler, (int)WindowLongFlags.GWL_STYLE, dwStyle);
                    SetWindowPlacement(handler, ref wp);
                    _fullscreen = false;
                }
                else
                {
                    GetWindowPlacement(handler, ref wp);
                    dwStyle = GetWindowLong(handler, (int)WindowLongFlags.GWL_STYLE);
                    SetWindowLong(handler, (int)WindowLongFlags.GWL_STYLE, (IntPtr)WindowStyles.WS_POPUP);
                    Rectangle rect = new Rectangle(0, 0, GetSystemMetrics(SystemMetrics.CXSCREEN), GetSystemMetrics(SystemMetrics.CYSCREEN));
                    SetWindowPos(handler, IntPtr.Zero, rect.X, rect.Y, rect.Width, rect.Height, SetWindowPosFlags.ShowWindow);
                    _fullscreen = true;
                }
            }
        }
        private class DevToolsWebClient : CefClient { }
        public void showDevTools()
        {
#if DEBUG
            if (!_config.DevToolsUrl.IsNullOrWhiteSpace())
            {
                if (_window.Browser is CefGlueBrowser)
                {
                    var browser = (_window.Browser as CefGlueBrowser).CefBrowser;
                    var host = browser.GetHost();
                    if (host.HasDevTools)
                    {
                        host.CloseDevTools();
                    }
                    else
                    {
                        var wi = CefWindowInfo.Create();
                        //wi.SetAsChild(_window.Handle, new CefRectangle(0, 0, _config.WindowOptions.Size.Width, _config.WindowOptions.Size.Height));
                        wi.SetAsPopup(_window.Handle, "DevTools");
                        host.ShowDevTools(wi, new DevToolsWebClient(), new CefBrowserSettings(), new CefPoint(0, 0));
                    }
                }
            }
#endif
        }
    }
}
